package com.dominator.repository;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.dominFramework.core.id.SystemIdUtils;
import com.dominFramework.core.utils.SystemUtils;
import com.dominator.mybatis.dao.T_userDao;
import com.dominator.mybatis.dao.T_userPO;
import com.dominator.utils.api.ApiUtils;
import com.dominator.utils.dao.JdbcHelper;
import com.dominator.utils.encode.Des3Utils;
import com.dominator.utils.system.PropertiesLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.dominFramework.core.dao.SqlDao;
import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.typewrap.Dtos;


@Repository
public class DaoUser {

	public static PropertiesLoader propertiesLoader = new PropertiesLoader("sysconfig.properties");


	@Autowired
	T_userDao userDao;
	
	@Resource
	JdbcHelper jh;
	
	@Autowired
	SqlDao sqlDao;
	

	/**
	 * 添加
	 * 
	 * @param record
	 * @return
	 */
	public Integer insertSelective(T_userPO record) {
		return userDao.insert(record);
	}

	/**
	 * 删除
	 * 
	 * @param
	 * @return
	 */
	public Integer deleteByPrimaryKey(String id) {
		return userDao.deleteByKey(id);
	}

	/**
	 * 修改
	 * 
	 * @param record
	 * @return
	 */
	public Integer updateByPrimaryKey(T_userPO record) {
		return userDao.updateByKey(record);
	}


	/**
	 * 查询单条记录
	 * 
	 * @param id
	 * @return
	 */
	public T_userPO selectByPrimaryKey(String id) {
		T_userPO row = userDao.selectByKey(id);
		return row;
	}

	/**
	 * 根据手机号查询单条记录
	 * 
	 * @param
	 * @return
	 */
	public T_userPO selectByPhoneNum(String phoneNum) {
		Dto qDto=Dtos.newDto("phone_num", phoneNum);
		T_userPO row = (T_userPO) sqlDao.selectOne("User.selectByPhoneNum", qDto);
		return row;
	}



	/**
	 * 根据email查询单条记录
	 *
	 * @param
	 * @return
	 */
	public T_userPO selectByEmail(String email) {
		Dto qDto=Dtos.newDto("email", email);
		T_userPO row = (T_userPO) sqlDao.selectOne("User.selectByEmail", qDto);
		return row;
	}


	/**
	 * 根据userName查询单条记录
	 *
	 * @param
	 * @return
	 */
	public T_userPO selectByUserName(String userName) {
		Dto qDto=Dtos.newDto("user_name", userName);
		T_userPO row = (T_userPO) sqlDao.selectOne("User.selectByUserName", qDto);
		return row;
	}






	/**
	 * 根据InviteCode查询单条记录
	 *
	 * @param
	 * @return
	 */
	public T_userPO selectByInviteCode(String inviteCode) {
		Dto qDto=Dtos.newDto("invite_code", inviteCode);
		T_userPO row = (T_userPO) sqlDao.selectOne("User.selectByInviteCode", qDto);
		return row;
	}





	/**
	 * 根据openId查询单条记录
	 *
	 * @param
	 * @return
	 */
	public T_userPO selectByWxOpenId(String wxOpenId) {
		Dto qDto=Dtos.newDto("wx_open_id", wxOpenId);
		T_userPO row = (T_userPO) sqlDao.selectOne("User.selectByWxOpenId", qDto);
		return row;
	}



	/**
	 * 根据qq号查询单条记录
	 * 
	 * @param
	 * @return
	 */
	public T_userPO selectByQq(String qq) {
		Dto qDto=Dtos.newDto("associated_qq", qq);
		T_userPO row = (T_userPO) sqlDao.selectOne("User.selectByQq", qDto);
		return row;
	}

	/**
	 * 根据微信查询单条记录
	 * 
	 * @param
	 * @return
	 */
	public T_userPO selectByWx(String wx) {
		Dto qDto=Dtos.newDto("associated_wx", wx);
		T_userPO row = (T_userPO) sqlDao.selectOne("User.selectByWx", qDto);
		return row;
	}
	
	/**
	 * 根据微博查询单条记录
	 * 
	 * @param
	 * @return
	 */
	public T_userPO selectByWb(String wb) {
		Dto qDto=Dtos.newDto("associated_wb", wb);
		T_userPO row = (T_userPO) sqlDao.selectOne("User.selectByWb", qDto);
		return row;
	}

	/**
	 * 根据手机号密码查询单条记录
	 * 
	 * @param
	 * @return
	 */
	public T_userPO selectByPhoneNumPassword(String phoneNum, String password) {
		Dto qDto=Dtos.newDto();
		qDto.put("phone_num", phoneNum);
		qDto.put("password", Des3Utils.encode(password));
		T_userPO row = (T_userPO) sqlDao.selectOne("User.selectByPhoneNumPassword", qDto);
		return row;
	}

	/**
	 * 创建qq用户
	 * 
	 * @param
	 * @param
	 * @param
	 * @return
	 */
	public Integer createQQUser(Dto dto) {
		T_userPO user = new T_userPO();
		String userId = SystemIdUtils.uuid();
		user.setId(userId);
		user.setGenter(dto.getString("genter"));
		user.setAssociated_qq(dto.getString("associatedQq"));
		user.setNick_name(dto.getString("nickName"));
		user.setCountry(dto.getString("country"));
		user.setProvince(dto.getString("province"));
		user.setCity(dto.getString("city"));
		user.setHead_img(SystemUtils.isNotEmpty(dto.getString("headImg"))?propertiesLoader.getProperty("default_img_url"):dto.getString("headImg"));
		user.setCreate_time(new Date());
		user.setIs_valid("1");
		return userDao.insert(user);

	}


	

	/**
	 * 创建手机用户
	 * 
	 * @param
	 * @param
	 * @param
	 * @return
	 */
	public Integer createPhoneUser(Dto dto) {
		T_userPO user = new T_userPO();
		String userId = SystemIdUtils.uuid();
		user.setId(userId);
		user.setNick_name("手机用户_" + dto.getString("phoneNum").substring(dto.getString("phoneNum").length() - 4,
				dto.getString("phoneNum").length()));
		user.setGenter("1");
		user.setPhone_num(dto.getString("phoneNum"));
		if (SystemUtils.isNotEmpty(dto.getString("password"))) {
			user.setPassword(Des3Utils.encode(dto.getString("password")));
		}
		if(SystemUtils.isNotEmpty(dto.getString("invitePhoneNum"))){
			T_userPO pUser=selectByPhoneNum(dto.getString("invitePhoneNum"));
			user.setPid(pUser.getId());
		}
		user.setMerchant_id(dto.getString("merchantId"));
		user.setPartner_id(dto.getString("partnerId"));
		user.setPid(dto.getString("pid"));
		user.setCountry(dto.getString("country"));
		user.setProvince(dto.getString("province"));
		user.setCity(dto.getString("city"));
		user.setHead_img(propertiesLoader.getProperty("default_img_url"));
		user.setCreate_time(new Date());
		user.setIs_valid("1");
		user.setAssociated_qq(dto.getString("associateQq"));
		user.setAssociated_wx(dto.getString("associateWx"));
		user.setWx_open_id(dto.getString("wxOpenId"));
		user.setMember_grade("1");
		return userDao.insert(user);

	}

	
	
	
	
	
	
	
	
	
	
	/**
	 * 查询邀请人数
	 * 
	 * @param userId
	 * @return
	 */
	public Integer countInvitePerson(String userId) {
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from t_user where phone_num is not NULL AND pid='" + userId + "' and is_valid='1'   and last_login_time is not null ");
		return jh.count(sb.toString(), null);
	}





	/**
	 * 根据pid查询记录
	 *
	 * @param
	 * @return
	 */
	public List<T_userPO> selectByPid(Dto dto) {
		List<T_userPO> list =  sqlDao.list("User.selectByDto", dto);
		return list;
	}


	/**
	 * 创建微信用户
	 *
	 * @param
	 * @param
	 * @param
	 * @return
	 */
	public Integer createWXUser(Dto dto) {
		T_userPO user = new T_userPO();
		String userId = SystemIdUtils.uuid();
		user.setId(userId);
		user.setNick_name(dto.getString("nickname"));
		user.setHead_img(dto.getString("headimgurl"));

		user.setCreate_time(new Date());
		user.setIs_valid("1");
		user.setWx_open_id(dto.getString("wxOpenId"));
		user.setMember_grade("1");
		return userDao.insert(user);

	}



	/**
	 * 创建Email用户
	 *
	 * @param
	 * @param
	 * @param
	 * @return
	 */
	public Integer createEmailUser(Dto dto) {
		while (true){
			String inviteCode=ApiUtils.getCharAndNumr(6);
			T_userPO tUserPO= selectByInviteCode(inviteCode);
            if (SystemUtils.isEmpty(tUserPO)){
                dto.put("inviteCode",inviteCode);
            	break;
			}
		}
		T_userPO user = new T_userPO();
		String userId = SystemIdUtils.uuid();
		user.setId(userId);
		user.setUser_name(dto.getString("userName"));
		user.setPassword(Des3Utils.encode(dto.getString("password")));
		user.setCreate_time(new Date());
		user.setIs_valid("1");
		user.setEmail(dto.getString("email"));
		user.setInvite_code(dto.getString("inviteCode"));
		return userDao.insert(user);

	}
	
	
	
	
	
	
	
	
}
